import os
import shutil
import re

def delete_regex(target, regex_str):
    """
    删除匹配正则表达式的文件/文件夹
    :param target: 要删除的文件/文件夹路径
    :param regex_str: 正则表达式字符串
    """

    try:
        if os.path.isfile(target):
            if re.search(regex_str, target):
                os.remove(target)
                print(f"已删除文件: {target}")
        elif os.path.isdir(target):
            for name in os.listdir(target):
                if re.search(regex_str, name):
                    path = os.path.join(target, name)
                    if os.path.isfile(path):
                        os.remove(path)
                        print(f"已删除文件: {path}")
                    elif os.path.isdir(path):
                        shutil.rmtree(path)
                        print(f"已删除文件夹: {path}")
        else:
            print(f"目标不存在: {target}")
    except Exception as e:
        print(f"删除失败: {target}: {e}")


def delete_specific(target):
    """
    删除指定文件/文件夹
    :param target: 要删除的文件/文件夹路径
    """
    try:
        if os.path.isfile(target):
            os.remove(target)
            print(f"已删除文件: {target}")
        elif os.path.isdir(target):
            shutil.rmtree(target)
            print(f"已删除文件夹: {target}")
        else:
            print(f"目标不存在: {target}")
    except Exception as e:
        print(f"删除失败: {target}: {e}")


def delete_targets(root_dir, targets):
    """
    递归删除指定目录下的目标文件/文件夹
    :param root_dir: 起始目录（当前目录用'.'）
    :param targets: 要删除的目标名称列表
    """
    # 遍历目录树
    for dirpath, dirnames, filenames in os.walk(root_dir, topdown=False):
        # 检查文件夹（dirnames）
        for dirname in dirnames:
            if dirname in targets:
                dir_path = os.path.join(dirpath, dirname)
                try:
                    # 删除文件夹（包括所有子内容）
                    shutil.rmtree(dir_path)
                    print(f"已删除文件夹: {dir_path}")
                except Exception as e:
                    print(f"删除文件夹失败 {dir_path}: {e}")

        # 检查文件（filenames）
        for filename in filenames:
            if filename in targets:
                file_path = os.path.join(dirpath, filename)
                try:
                    # 删除文件
                    os.remove(file_path)
                    print(f"已删除文件: {file_path}")
                except Exception as e:
                    print(f"删除文件失败 {file_path}: {e}")


if __name__ == "__main__":
    # 要删除的目标列表（可根据需要添加）
    targets_to_delete = [
        "vendor",
        "composer.lock",
        "generated",
        "node_modules",
        "dist",
        "package-lock.json",
        "yarn.lock",
        ".cache",
        "site-packages",
        "dev.db",
    ]
    specific_targets = [
        r"C:/Users/yong/AppData/Local/go-build",
        r"C:\Users\yong\AppData\Local\QQChess",
        r"C:\Users\yong\AppData\Local\TortoiseGit",
        r"C:\Users\yong\AppData\Roaming\Cursor",
        r"C:\Users\yong\AppData\Roaming\Stirling-PDF",
        r"C:\Users\yong\AppData\Roaming\com.wonderidea.MicroVideo",
        r"C:\Users\yong\vscode-remote-wsl\stable",
        r"C:\Users\yong\AppData\Roaming\qqchess",
        r"d:/wx/vue-learning-demo/doc-web/vite-docs/.vitepress/cache",
        r"d:/wx/vue-learning-demo/doc-web/vue-docs/docs/.vuepress/.temp",
        r"d:/wx/vue-learning-demo/go-demo/gin/tmp",
    ]

    # 从当前目录开始删除
    # delete_targets(".", targets_to_delete)
    # delete_targets("./doc-web", targets_to_delete)
    # delete_specific(r"C:/Users/yong/AppData/Local/go-build")
    for target in specific_targets:
        delete_specific(target)
    # delete_regex(r"C:/Users/yong/AppData/Local/Temp", r"go-build.*")
    print("删除操作完成")

